package com.ltf.config;

import com.ltf.Utils.PackJson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class JwtAuthEntryPoint
      implements AuthenticationEntryPoint {
    Logger logger=LoggerFactory.getLogger(this.getClass());
    @Override
    public void commence(HttpServletRequest request,
                         HttpServletResponse response,
                         AuthenticationException authException) throws IOException, ServletException {
        logger.info(authException.getMessage());
        //403表示权限不足  401用户登陆失败（身份验证失败，即authenticaiton失败）
        response.setStatus(401);
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json");
        response.getWriter().println(PackJson.packJson(401,"权限不足，无法访问",authException.getMessage()));
        response.flushBuffer();//刷新

    }
}
